using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CollectiblePool : MonoBehaviour
{
	private static CollectiblePool instance;

	public Collectible collectiblePrefab;

	private Queue<Collectible> collectibles;

	private Collectible tempCollectible;

	private Vector3 initialPos;

	private int colCount;

	private List<Collectible> initializeList;

	private int spawnCount;

	private int explotionCount;

	private HashSet<Collectible> collectiblesOnField;

	private List<Collectible> returningTemp;

	public static CollectiblePool Instance
	{
		get
		{
			return instance;
		}
	}

	private void Awake()
	{
		if (instance == null)
		{
			instance = this;
		}
		else
		{
			Object.Destroy(base.gameObject);
		}
	}

	private void Start()
	{
		collectibles = new Queue<Collectible>();
		initialPos = new Vector3(-1000f, -1000f, 0f);
		collectiblesOnField = new HashSet<Collectible>();
		returningTemp = new List<Collectible>();
		spawnCount = 1000;
		explotionCount = 750;
		initializeList = new List<Collectible>();
		for (int i = 0; i < spawnCount; i++)
		{
			SpawnCollectible();
		}
		for (int j = 0; j < explotionCount; j++)
		{
			collectibles.Enqueue(CreateCollectible());
		}
		StartCoroutine(SpawnCollectibleRoutine());
	}

	private void FixedUpdate()
	{
		if (initializeList.Count > 0)
		{
			for (int i = 0; i < initializeList.Count; i++)
			{
				initializeList[i].SetPosition();
			}
			initializeList.Clear();
		}
	}

	public Collectible CreateCollectible()
	{
		colCount++;
		return Object.Instantiate(collectiblePrefab, initialPos, Quaternion.identity, base.transform);
	}

	public void ReturnCollectible(Collectible collectible)
	{
		collectible.transform.position = initialPos;
		collectible.transform.localScale = Vector3.zero;
		collectibles.Enqueue(collectible);
		collectiblesOnField.Remove(collectible);
	}

	public Collectible GetCollectible()
	{
		if (collectibles.Count == 0 && colCount < spawnCount + explotionCount)
		{
			tempCollectible = CreateCollectible();
			initializeList.Add(tempCollectible);
			return tempCollectible;
		}
		if (collectibles.Count > 0)
		{
			tempCollectible = collectibles.Dequeue();
			collectiblesOnField.Add(tempCollectible);
			initializeList.Add(tempCollectible);
			return tempCollectible;
		}
		int num = 0;
		foreach (Collectible item in collectiblesOnField)
		{
			if (Vector2.Distance(item.transform.position, FollowingCamera.instance.transform.position) > 15f)
			{
				returningTemp.Add(item);
				num++;
			}
			if (num == 300)
			{
				break;
			}
		}
		for (int i = 0; i < returningTemp.Count; i++)
		{
			// returningTemp[i].Finalize();
		}
		returningTemp.Clear();
		if (collectibles.Count > 0)
		{
			tempCollectible = collectibles.Dequeue();
			collectiblesOnField.Add(tempCollectible);
			initializeList.Add(tempCollectible);
			return tempCollectible;
		}
		return null;
	}

	private IEnumerator SpawnCollectibleRoutine()
	{
		while (true)
		{
			yield return new WaitForSeconds(0.01f);
			if (collectibles.Count > spawnCount)
			{
				SpawnCollectible();
			}
		}
	}

	private void SpawnCollectible()
	{
		tempCollectible = GetCollectible();
		if (tempCollectible != null)
		{
			tempCollectible.Initialize(1, 1, GameManager.Instance.RandomColor());
		}
	}
}
